/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package restauranteaplicacion.Ingresos;
import restauranteaplicacion.Modificaciones.ModificacionProducto;
import entidad.Unidaddemedida;
import CapaNegocios.CNUnidadMedida;
import CapaNegocios.CNProducto;
import Clases.Observador;
import java.awt.Component;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JOptionPane;
import restauranteaplicacion.MDI;
import Clases.ComboBox;
import entidad.Producto;
import java.awt.Color;
import CapaNegocios.Conversor;
/**
 *
 * @author a
 */
public class IngresoProducto extends javax.swing.JInternalFrame implements Observer{

    /**
     * Creates new form IngresoProducto
     */
    Observador obs; 
    Observador obsPlato;
    List<Unidaddemedida> lista;
    //variables para ver el cambio de Unidad de Medida en comboBox para modificaciones
    boolean comboBox= false;
    public IngresoProducto() {
        initComponents();
        actualizarUnidadMedida();
        comboBox=false;
        obs = new Observador();
        obs.addObserver(this);
        obsPlato=null;
        producto1=null;
    }
    
    public IngresoProducto(Observador obse) {
        initComponents();
        comboBox=false;
        actualizarUnidadMedida();
        obs = new Observador();
        obs.addObserver(this);
        obsPlato = obse;
        producto1=null;
    }
    
    public IngresoProducto(Observador obse, Producto pro) {
        //producto1 = pro;
        initComponents();
        comboBox=false;
        actualizarUnidadMedida(pro.getUnidadDeMedidaidUnidadDeMedida().getTipo());
        obs = new Observador();
        obs.addObserver(this);
        ingresarProducto(pro,producto1);
        ComboBox com = new ComboBox(producto1.getUnidadDeMedidaidUnidadDeMedida().getIdUnidadDeMedida(),
                producto1.getUnidadDeMedidaidUnidadDeMedida().getNombre());
        cmbUnidadMedida.setSelectedItem(com);
        obsPlato = obse;  
        comboBox=true;
    }
    
    private void ingresarProducto(Producto viejoPro, Producto productoNuevo )
    {
        productoNuevo.setNombre(viejoPro.getNombre());
        productoNuevo.setActivo(viejoPro.getActivo());
        productoNuevo.setCanitdad(viejoPro.getCanitdad());
        productoNuevo.setCodigo(viejoPro.getCodigo());
        productoNuevo.setDescripcion(viejoPro.getDescripcion());
        productoNuevo.setDescuentoAutomatico(viejoPro.getDescuentoAutomatico());
        productoNuevo.setIdProducto(viejoPro.getIdProducto());
        productoNuevo.setMinimaExistencias(viejoPro.getMinimaExistencias());
        productoNuevo.setPrecio(viejoPro.getPrecio());
        productoNuevo.setPresentacion(viejoPro.getPresentacion());
        productoNuevo.setUnidadDeMedidaidUnidadDeMedida(viejoPro.getUnidadDeMedidaidUnidadDeMedida());  
        productoNuevo.setBebidasHasProductoCollection(viejoPro.getBebidasHasProductoCollection());
        productoNuevo.setDetalleDePlatoCollection(viejoPro.getDetalleDePlatoCollection());
        productoNuevo.setDetalleordensalidaCollection(viejoPro.getDetalleordensalidaCollection());
        productoNuevo.setLotesCollection(viejoPro.getLotesCollection());
    }
    
    private void actualizarUnidadMedida()
    {
        cmbUnidadMedida.removeAllItems();
        lista = CNUnidadMedida.buscarTodos();
        if(lista!=null)
            for(Unidaddemedida x : lista)
            {
                cmbUnidadMedida.addItem(new ComboBox(x.getIdUnidadDeMedida(), x.getNombre()));
            }
    }
    
    private void actualizarUnidadMedida(String tipo )
    {
        cmbUnidadMedida.removeAllItems();
        lista = CNUnidadMedida.buscarTipo(tipo);
        if(lista!=null)
            for(Unidaddemedida x : lista)
            {
                cmbUnidadMedida.addItem(new ComboBox(x.getIdUnidadDeMedida(), x.getNombre()));
            }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        producto1 = new entidad.Producto();
        jPanel1 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        txtPrecio = new javax.swing.JTextField();
        txtCantidaMinima = new javax.swing.JTextField();
        btnCancelar = new javax.swing.JButton();
        btnGuardar = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        taDescripcion = new javax.swing.JTextArea();
        cmbUnidadMedida = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        txtPresentacion = new javax.swing.JTextField();
        txtCantidad = new javax.swing.JTextField();

        setClosable(true);
        setIconifiable(true);
        setTitle("Ingreso de Productos");
        setMaximumSize(new java.awt.Dimension(666, 557));
        setMinimumSize(new java.awt.Dimension(666, 552));
        setPreferredSize(new java.awt.Dimension(666, 557));
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
        });
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.PAGE_AXIS));

        jPanel1.setBackground(new java.awt.Color(54, 149, 240));
        jPanel1.setMaximumSize(new java.awt.Dimension(650, 50));
        jPanel1.setMinimumSize(new java.awt.Dimension(500, 50));
        jPanel1.setPreferredSize(new java.awt.Dimension(650, 50));

        jLabel5.setFont(new java.awt.Font("Arial", 1, 24)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setText("Creacion de Producto");
        jPanel1.add(jLabel5);

        getContentPane().add(jPanel1);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setPreferredSize(new java.awt.Dimension(666, 548));

        jLabel1.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jLabel1.setText("Nombre");

        jLabel2.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jLabel2.setText("Precio");

        jLabel3.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jLabel3.setText("Cantidad");

        jLabel4.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jLabel4.setText("Cantidad Minima");

        txtNombre.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txtNombre.setNextFocusableComponent(txtCodigo);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, producto1, org.jdesktop.beansbinding.ELProperty.create("${nombre}"), txtNombre, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        txtNombre.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtNombreFocusGained(evt);
            }
        });

        txtPrecio.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txtPrecio.setNextFocusableComponent(txtCantidad);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, producto1, org.jdesktop.beansbinding.ELProperty.create("${precio}"), txtPrecio, org.jdesktop.beansbinding.BeanProperty.create("text"), "");
        binding.setSourceNullValue("0");
        bindingGroup.addBinding(binding);

        txtPrecio.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtPrecioFocusGained(evt);
            }
        });
        txtPrecio.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtPrecioKeyTyped(evt);
            }
        });

        txtCantidaMinima.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txtCantidaMinima.setNextFocusableComponent(cmbUnidadMedida);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, producto1, org.jdesktop.beansbinding.ELProperty.create("${minimaExistencias}"), txtCantidaMinima, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        txtCantidaMinima.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtCantidaMinimaFocusGained(evt);
            }
        });
        txtCantidaMinima.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtCantidaMinimaKeyTyped(evt);
            }
        });

        btnCancelar.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/restauranteaplicacion/Ingresos/circle-x-3x.png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnCancelar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnGuardar.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/circle-check-3x.png"))); // NOI18N
        btnGuardar.setText("Guardar");
        btnGuardar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnGuardar.setPreferredSize(new java.awt.Dimension(89, 53));
        btnGuardar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jLabel8.setText("Descripcion");

        jScrollPane1.setPreferredSize(new java.awt.Dimension(166, 68));

        taDescripcion.setColumns(20);
        taDescripcion.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        taDescripcion.setRows(3);
        taDescripcion.setNextFocusableComponent(btnGuardar);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, producto1, org.jdesktop.beansbinding.ELProperty.create("${descripcion}"), taDescripcion, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        taDescripcion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                taDescripcionKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                taDescripcionKeyTyped(evt);
            }
        });
        jScrollPane1.setViewportView(taDescripcion);

        cmbUnidadMedida.setNextFocusableComponent(taDescripcion);
        cmbUnidadMedida.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbUnidadMedidaActionPerformed(evt);
            }
        });

        jLabel9.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jLabel9.setText("Unidad De Medida");

        jButton2.setBackground(new java.awt.Color(255, 255, 255));
        jButton2.setForeground(new java.awt.Color(102, 102, 255));
        jButton2.setText("+");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jLabel7.setText("Presentacion");

        jLabel6.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jLabel6.setText("Codigo");

        txtCodigo.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txtCodigo.setNextFocusableComponent(txtPresentacion);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, producto1, org.jdesktop.beansbinding.ELProperty.create("${codigo}"), txtCodigo, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        txtCodigo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCodigoActionPerformed(evt);
            }
        });
        txtCodigo.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtCodigoFocusGained(evt);
            }
        });
        txtCodigo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtCodigoKeyTyped(evt);
            }
        });

        txtPresentacion.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txtPresentacion.setNextFocusableComponent(txtPrecio);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, producto1, org.jdesktop.beansbinding.ELProperty.create("${presentacion}"), txtPresentacion, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        txtPresentacion.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtPresentacionFocusGained(evt);
            }
        });

        txtCantidad.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txtCantidad.setNextFocusableComponent(txtCantidaMinima);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, producto1, org.jdesktop.beansbinding.ELProperty.create("${canitdad}"), txtCantidad, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        txtCantidad.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtCantidadFocusGained(evt);
            }
        });
        txtCantidad.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtCantidadKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(54, 54, 54)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(62, 62, 62)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtPrecio)
                    .addComponent(txtCodigo)
                    .addComponent(txtNombre)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(cmbUnidadMedida, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton2)))
                        .addGap(0, 49, Short.MAX_VALUE))
                    .addComponent(txtPresentacion)
                    .addComponent(txtCantidad)
                    .addComponent(txtCantidaMinima))
                .addGap(116, 116, 116))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(482, 482, 482)
                        .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addComponent(btnCancelar)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtPresentacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtPrecio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtCantidaMinima, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(cmbUnidadMedida, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCancelar))
                .addContainerGap(50, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel2);

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        // TODO add your handling code here:
        if(CNProducto.codigoExistente(txtCodigo.getText())){
                JOptionPane.showMessageDialog(this, "Codigo Incorrecto, El codigo "+ txtCodigo.getText()+" ya existe en la base de datos", 
                        "Informacion", JOptionPane.ERROR_MESSAGE); 
                txtCodigo.requestFocus(true);
                return;
        }
        if(validar())
        {
            ComboBox combo= (ComboBox) cmbUnidadMedida.getSelectedItem();
            Unidaddemedida unida= CNUnidadMedida.buscarId(combo.getId());
             try{
                if(producto1 != null)
                {
                    
                    producto1.setUnidadDeMedidaidUnidadDeMedida(unida);
                    if(CNProducto.modificar(producto1))
                    {
                        JOptionPane.showMessageDialog(this, "Se ha modificado correctamente", 
                                "Informacion", JOptionPane.INFORMATION_MESSAGE);
                        ingresarProducto(producto1, ModificacionProducto.producto2);
                        if(obsPlato!=null)
                            obsPlato.actualizar();
                        this.setVisible(false);
                    }
                    else        
                    {
                        JOptionPane.showMessageDialog(this, "No se ha modificado correctamente", 
                                "Informacion", JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                }
                else
                {
                    CNProducto.insertar(txtNombre.getText(), Float.valueOf(txtPrecio.getText()),
                            Float.valueOf(txtCantidad.getText()),Integer.valueOf(txtCantidaMinima.getText()),
                            taDescripcion.getText(),true,txtPresentacion.getText(),unida.getIdUnidadDeMedida());
                    JOptionPane.showMessageDialog(this, "Se ha guardado correctamente",
                            "Informacion", JOptionPane.INFORMATION_MESSAGE);
                }
                limpiar();
                if(obsPlato!=null)
                {
                    obsPlato.actualizar();
                     this.setVisible(false);
                }
             }catch(Exception e)
            {
                JOptionPane.showMessageDialog(this, "No se ha guardado correctamente", "Informacion", JOptionPane.ERROR_MESSAGE);
                e.getMessage();
            }            
        }else
          JOptionPane.showMessageDialog(this, "No se ha guardado correctamente porfavor ingresar todos los datos", "Informacion", JOptionPane.ERROR_MESSAGE);  
    }//GEN-LAST:event_btnGuardarActionPerformed

    public boolean validar(){
        boolean var = true;
        if(txtCantidad.getText().equals(""))
            txtCantidad.setText("0");
        if(txtPrecio.getText().equals(""))
            txtPrecio.setText("0");
        if(txtNombre.getText().equals(""))
        {
            txtNombre.setBackground(Color.getHSBColor(255,204,204));
            var=false;
        }
        if(txtCantidaMinima.getText().equals(""))
        {
            txtCantidaMinima.setBackground(Color.getHSBColor(255, 204, 204));
            var=false;
        }
        return var;
    }
    public void limpiar()
    {
        txtNombre.setText("");
        txtCantidad.setText("");
        txtPrecio.setText("");
        txtCantidaMinima.setText("");
        taDescripcion.setText("");
        txtPresentacion.setText("");
        txtNombre.requestFocus();
    }
    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        if(obsPlato!=null)
            obsPlato.actualizar();
        this.setVisible(false);
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        UnidadDeMedida ing = new UnidadDeMedida(obs);
        MDI.getDesktopPane().add(ing);
        ing.show();
        obs.addObserver(ing);
        this.Desactivar();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void txtCantidaMinimaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCantidaMinimaKeyTyped
        // TODO add your handling code here:
        char c=evt.getKeyChar();  
        if((!Character.isDigit(c))&&(c != '.')) { 
              getToolkit().beep();                
              evt.consume(); 
          }
    }//GEN-LAST:event_txtCantidaMinimaKeyTyped

    private void txtCantidaMinimaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCantidaMinimaFocusGained
        // TODO add your handling code here:
        Object o = evt.getSource(); 
        if(o instanceof javax.swing.JTextField)
        { 
            javax.swing.JTextField txt = (javax.swing.JTextField) o; 
            txt.setSelectionStart(0); 
            txt.setSelectionEnd(txt.getText().length()); 
        }
        txtCantidaMinima.setBackground(Color.WHITE);
    }//GEN-LAST:event_txtCantidaMinimaFocusGained

    private void txtCodigoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCodigoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCodigoActionPerformed

    private void txtCodigoFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCodigoFocusGained
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCodigoFocusGained

    private void txtCodigoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCodigoKeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCodigoKeyTyped

    private void txtNombreFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtNombreFocusGained
        // TODO add your handling code here:
        txtNombre.setBackground(Color.WHITE);
    }//GEN-LAST:event_txtNombreFocusGained

    private void txtPrecioKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPrecioKeyTyped
        // TODO add your handling code here:
        char c=evt.getKeyChar();
        if((!Character.isDigit(c))&&(c != '.')) {
            getToolkit().beep();
            evt.consume();
        }
    }//GEN-LAST:event_txtPrecioKeyTyped

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
        // TODO add your handling code here:
        if(obsPlato!=null)
            obsPlato.actualizar();
        this.setVisible(false);        
    }//GEN-LAST:event_formInternalFrameClosing

    private void txtPresentacionFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtPresentacionFocusGained
        // TODO add your handling code here:
        txtPresentacion.setBackground(Color.WHITE);
    }//GEN-LAST:event_txtPresentacionFocusGained

    private void txtCantidadFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCantidadFocusGained
        // TODO add your handling code here:
        txtCantidad.setBackground(Color.WHITE);
        Object o = evt.getSource(); 
        if(o instanceof javax.swing.JTextField)
        { 
            javax.swing.JTextField txt = (javax.swing.JTextField) o; 
            txt.setSelectionStart(0); 
            txt.setSelectionEnd(txt.getText().length()); 
        }
    }//GEN-LAST:event_txtCantidadFocusGained

    private void txtCantidadKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCantidadKeyTyped
        // TODO add your handling code here:
        char c=evt.getKeyChar();  
        if((!Character.isDigit(c))&&(c != '.')) { 
              getToolkit().beep();                
              evt.consume(); 
          }
    }//GEN-LAST:event_txtCantidadKeyTyped

    private void txtPrecioFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtPrecioFocusGained
        // TODO add your handling code here:
        Object o = evt.getSource(); 
        if(o instanceof javax.swing.JTextField)
        { 
            javax.swing.JTextField txt = (javax.swing.JTextField) o; 
            txt.setSelectionStart(0); 
            txt.setSelectionEnd(txt.getText().length()); 
        }
        txtPrecio.setBackground(Color.WHITE);
    }//GEN-LAST:event_txtPrecioFocusGained

    private void cmbUnidadMedidaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbUnidadMedidaActionPerformed
        // TODO add your handling code here:
        if(comboBox && cmbUnidadMedida.getSelectedItem()!=null)
        {
            ComboBox combo= (ComboBox) cmbUnidadMedida.getSelectedItem();
            Unidaddemedida unida= CNUnidadMedida.buscarId(combo.getId());            
           if(producto1.getUnidadDeMedidaidUnidadDeMedida().getIdUnidadDeMedida() != unida.getIdUnidadDeMedida())
           {                
                //Se cambiaran la cantidad de medida por el cambio que se realizo
                float x = Conversor.convertirProducto(unida,producto1.getUnidadDeMedidaidUnidadDeMedida(), producto1.getCanitdad());
                float cantidaMinima= Conversor.convertirProducto(unida, producto1.getUnidadDeMedidaidUnidadDeMedida(), producto1.getMinimaExistencias());
                float precio = Conversor.convertirProducto( producto1.getUnidadDeMedidaidUnidadDeMedida(),unida, producto1.getPrecio());
                producto1.setCanitdad(x);
                producto1.setMinimaExistencias((int) cantidaMinima);          
                producto1.setPrecio(precio);
                producto1.setUnidadDeMedidaidUnidadDeMedida(unida);
                txtPrecio.setBackground(Color.green);
                txtCantidad.setBackground(Color.green);
                txtCantidaMinima.setBackground(Color.green);
           } 
        }
    }//GEN-LAST:event_cmbUnidadMedidaActionPerformed

    private void taDescripcionKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_taDescripcionKeyTyped
        // TODO add your handling code here:
        
    }//GEN-LAST:event_taDescripcionKeyTyped

    private void taDescripcionKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_taDescripcionKeyPressed
        // TODO add your handling code here:
        int x = evt.getKeyCode();
        if(x==9){               
              evt.consume();
              btnGuardar.requestFocus(true);
          }
    }//GEN-LAST:event_taDescripcionKeyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JComboBox cmbUnidadMedida;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private entidad.Producto producto1;
    private javax.swing.JTextArea taDescripcion;
    private javax.swing.JTextField txtCantidaMinima;
    private javax.swing.JTextField txtCantidad;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtPrecio;
    private javax.swing.JTextField txtPresentacion;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    private void Desactivar()
    {
        for(Component x :this.jPanel2.getComponents())
        {
            x.setEnabled(false);
        }
    }
    private void Activar()
    {
        for(Component x :this.jPanel2.getComponents())
        {
            x.setEnabled(true);
        }
        
    }
    
    @Override
    public void update(Observable o, Object arg) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        actualizarUnidadMedida();
        Activar();
    }
}
